﻿using MiniSoftware;
using System.Text;
using 批量生成书面汇报;

Console.OutputEncoding = Encoding.UTF8;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

string templatePath = Path.Combine("Templates", "WeeklyReportTemplate.docx");
string outputDir = Path.Combine("Output");
Directory.CreateDirectory(outputDir);

var knowledgeWeeks = KnowledgeData.GetWeeklyKnowledgePoints();

// 起始日期：2025年10月25日，修正为该日期所在周的周一（确保每周从周一开始）
DateTime startDate = new DateTime(2025, 10, 25);
int daysToFirstMonday = startDate.DayOfWeek == DayOfWeek.Monday
    ? 0
    : (startDate.DayOfWeek == DayOfWeek.Sunday ? 1 : 8 - (int)startDate.DayOfWeek);
startDate = startDate.AddDays(daysToFirstMonday);

Random random = new();

for (int i = 0; i < knowledgeWeeks.Count; i++)
{
    var weekInfo = knowledgeWeeks[i];

    // 计算当前周的起始日（周一）和结束日（周日）
    DateTime startOfWeek = startDate.AddDays(7 * i); // 每次累加1周（确保是周一）
    DateTime endOfWeek = startOfWeek.AddDays(6);  // 周日

    // 随机生成本周内的书写日期（周一至周日）
    int offset = random.Next(0, 7); // 0-6天偏移，确保在本周内
    DateTime writeDate = startOfWeek.AddDays(offset);

    // 处理跨月情况：若书写日期超出当月，强制设为当月最后一天
    if (writeDate.Month != startOfWeek.Month)
    {
        writeDate = new DateTime(
            startOfWeek.Year,
            startOfWeek.Month,
            DateTime.DaysInMonth(startOfWeek.Year, startOfWeek.Month)
        );
    }

    // 中文显示用（模板内容）
    string month = startOfWeek.ToString("yyyy年MM月");
    int weekOfMonth = GetWeekOfMonth(startOfWeek);
    string reportTime = $"{month} 第{ToChineseNumber(weekOfMonth)}周";

    // 文件名中使用数字
    string fileSafeMonth = startOfWeek.ToString("yyyy年MM月");
    string fileName = $"【{fileSafeMonth} 第{weekOfMonth}周】社区矫正对象书面汇报.docx";

    // 文件输出路径
    string outputPath = Path.Combine(outputDir, fileName);

    // 日期格式
    string writeDateStr = $"{writeDate:yyyy 年 MM 月 dd 日}";

    var data = new
    {
        ReportTime = reportTime,
        Point1 = weekInfo[0],
        Point2 = weekInfo[1],
        Point3 = weekInfo[2],
        WriteDate = writeDateStr
    };

    await MiniWord.SaveAsByTemplateAsync(outputPath, templatePath, data);
    Console.WriteLine($"✅ 已生成：{fileName} ；第{weekOfMonth}周：{startOfWeek:yyyy-MM-dd}至{endOfWeek:yyyy-MM-dd}，书写日期：{writeDate:yyyy-MM-dd}");
}

Console.WriteLine("🎉 全部文档生成完毕！");


static int GetWeekOfMonth(DateTime date)
{
    // 每周从周一开始（调整DayOfWeek：周一=0，周日=6）
    int adjustedDayOfWeek = date.DayOfWeek == DayOfWeek.Sunday ? 6 : (int)date.DayOfWeek - 1;

    // 计算当月第一个周一的日期
    DateTime firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
    int firstAdjusted = firstDayOfMonth.DayOfWeek == DayOfWeek.Sunday ? 6 : (int)firstDayOfMonth.DayOfWeek - 1;
    DateTime firstMonday = firstDayOfMonth.AddDays(firstAdjusted == 0 ? 0 : 7 - firstAdjusted);

    // 如果当月1日是周日，第一个周一为1+1天（即2日）
    if (firstDayOfMonth.DayOfWeek == DayOfWeek.Sunday)
        firstMonday = firstDayOfMonth.AddDays(1);

    // 若日期在第一个周一之前，属于第1周
    if (date < firstMonday)
        return 1;

    // 计算当前日期距离第一个周一的天数，除以7取整+1（得到周数）
    int daysSinceFirstMonday = (date - firstMonday).Days;
    int weekOfMonth = (daysSinceFirstMonday / 7) + 1;

    return Math.Min(weekOfMonth, 5); // 最多5周
}


/// <summary>
/// 将数字转换为中文序号
/// </summary>
static string ToChineseNumber(int num)
{
    return num switch
    {
        1 => "一",
        2 => "二",
        3 => "三",
        4 => "四",
        5 => "五",
        _ => num.ToString()
    };
}